﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using 聚欣订单分析.PublicClass;

namespace 聚欣订单分析
{
    public partial class DataTotal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //divGly.Visible = false;
            if (!IsPostBack)
            {
                Page.Title = " 订单分析系统";
            }
            if (Session["qx"] == null)
            {
                Response.Redirect("login.aspx");
            }
            if (Request.QueryString["cx"].ToString() == "1")
            {
                if (Session["qx"].ToString() == "0")
                {
                    this.SqlDataSource1.SelectCommand = "SELECT * ,(ISNULL(订单金额,0)-ISNULL(开票金额,0)) as 未开金额,year(开票日期) as 开票年,MONTH(开票日期) as 开票月,MONTH(订单日期) as 订单月,MONTH(第一笔回款日期) as 回款月一,MONTH(第二笔回款日期) as 回款月二,(第一笔回款金额+第二笔回款金额) as 已回款 FROM [v_DDB] where (第一笔回款日期>='" + Session["HKRQ1"].ToString() + "' and 第一笔回款日期<='" + Session["HKRQ2"].ToString() + "') or (第二笔回款日期>='" + Session["HKRQ1"].ToString() + "' and 第二笔回款日期<='" + Session["HKRQ2"].ToString() + "') ORDER BY ID desc";
                }
                else
                {
                    this.ASPxGridView1.Columns[1].Visible = false;
                    string strWhere = "(第一笔回款日期>='" + Session["HKRQ1"].ToString() + "' and 第一笔回款日期<='" + Session["HKRQ2"].ToString() + "') or (第二笔回款日期>='" + Session["HKRQ1"].ToString() + "' and 第二笔回款日期<='" + Session["HKRQ2"].ToString() + "') and 省 in('" + Session["Qy"].ToString().Replace(",", "','") + "')";
                    this.SqlDataSource1.SelectCommand = "SELECT  * ,(ISNULL(订单金额,0)-ISNULL(开票金额,0)) as 未开金额,year(开票日期) as 开票年,MONTH(开票日期) as 开票月,MONTH(订单日期) as 订单月,MONTH(第一笔回款日期) as 回款月一,MONTH(第二笔回款日期) as 回款月二,(第一笔回款金额+第二笔回款金额) as 已回款 FROM [v_DDB] where " + strWhere + " ORDER BY ID desc";
                }
            }
            else
            {
                if (Session["qx"].ToString() == "0")
                {
                    this.SqlDataSource1.SelectCommand = "SELECT * ,(ISNULL(订单金额,0)-ISNULL(开票金额,0)) as 未开金额,year(开票日期) as 开票年,MONTH(开票日期) as 开票月,MONTH(订单日期) as 订单月,MONTH(第一笔回款日期) as 回款月一,MONTH(第二笔回款日期) as 回款月二,(第一笔回款金额+第二笔回款金额) as 已回款 FROM [v_DDB] ORDER BY ID desc";
                }
                else
                {
                    this.ASPxGridView1.Columns[1].Visible = false;
                    string strWhere = "省 in('" + Session["Qy"].ToString().Replace(",", "','") + "')";
                    this.SqlDataSource1.SelectCommand = "SELECT  * ,(ISNULL(订单金额,0)-ISNULL(开票金额,0)) as 未开金额,year(开票日期) as 开票年,MONTH(开票日期) as 开票月,MONTH(订单日期) as 订单月,MONTH(第一笔回款日期) as 回款月一,MONTH(第二笔回款日期) as 回款月二,(第一笔回款金额+第二笔回款金额) as 已回款 FROM [v_DDB] where " + strWhere + " ORDER BY ID desc";
                }
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            //   this.ASPxGridView1.Columns["备注"].Visible = true;
            ASPxGridViewExporter1.WriteXlsxToResponse("DataTotal");
        }

        //protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridViewTableDataCellEventArgs e)
        //{
        //    string cs = e.GetValue("订单号").ToString();
        //    string wkpcs = e.GetValue("wkpcs").ToString();
        //    if (cs != "0")
        //    {
        //        e.Cell.BackColor = System.Drawing.Color.Pink;
        //    }
        //    if (wkpcs != "0")
        //    {
        //        e.Cell.BackColor = System.Drawing.Color.PaleGreen;
        //    }
        //}


        protected void btnHkCx_Click(object sender, EventArgs e)
        {
            if (deKHrq1.Text != "" && deKHrq2.Text != "")
            {
                Session["HKRQ1"] = deKHrq1.Date.ToString("yyyy-MM-dd") + " 00:00:00";
                Session["HKRQ2"] = deKHrq2.Date.ToString("yyyy-MM-dd") + " 23:59:59";
                Response.Redirect("DataTotal.aspx?cx=1");
            }
            else
            {
                JsHelper.Alert("请选择日期范围！");
            }
        }

    }
}
